using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using EntLibEC.Biz.Basic;

namespace EntLibEC.View.Cmn
{
	/// <summary>
	/// Summary description for StockProductPickUp.
	/// </summary>
	public partial class StockProductPickUp : PageBase
	{
		protected EntLibEC.View.Cmn.Stock ucStock;
		protected string bodyOnLoad;
		private string clientID;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.QueryString["clientID"] != null )
			{
				clientID = Request.QueryString["clientID"].ToString();
			}
			if ( Request.QueryString["Sysno"] != null )
			{
				int sysno = Convert.ToInt32(Request.QueryString["Sysno"].ToString());
				ProductBasicInfo oInfo = ProductManager.GetInstance().LoadBasicBrief(sysno);
				bodyOnLoad = "javascript:window.opener.document.all['" + this.clientID + ":txtProductID'].value='" + oInfo.ProductID + "';";
				bodyOnLoad += "window.opener.document.all['" + this.clientID + ":txtProductSysNo'].value='" + oInfo.SysNo + "';";
				bodyOnLoad += "window.close(); return false;";
			}
			if ( !Page.IsPostBack)
			{
				ucStock.BindStock(true);
                ucCategory.BindCategory();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
            //if ( txtProductMisc.Text.Trim() == "")
            //{
            //    Assert(lblMessage, "please fill in something.", -1);
            //    return;
            //}
            Hashtable ht = new Hashtable();

            if (ucCategory.C3SysNo != AppConst.IntNull)
                ht.Add("c3sysno", ucCategory.C3SysNo);
            else if (ucCategory.C2SysNo != AppConst.IntNull)
                ht.Add("c2sysno", ucCategory.C2SysNo);
            else if (ucCategory.C1SysNo != AppConst.IntNull)
                ht.Add("c1sysno", ucCategory.C1SysNo);

			DataSet ds = ProductManager.GetInstance().GetBasicBriefDs(txtProductMisc.Text.Trim(),ucStock.StockSysNo,ht);
			DataGrid1.DataSource = ConvertDs(ds);
			DataGrid1.DataBind();
			int rows = 0;
			if ( Util.HasMoreRow(ds) )
				rows = ds.Tables[0].Rows.Count;
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}


		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds) )
				return ds;
			foreach(DataRow dr in ds.Tables[0].Rows)
			{
				dr["ProductName"] = "<a href='StockProductPickUp.aspx?Sysno=" + dr["Sysno"].ToString() +"&clientID=" + this.clientID + "'>" + dr["productName"].ToString() + "</a>";
			}
			return ds;
		}
	}
}
